package com.mfh.auto.create.mvvm.autocreatemvvm.file.v1

import com.intellij.psi.PsiDirectory
import com.mfh.auto.create.mvvm.autocreatemvvm.Config
import com.mfh.auto.create.mvvm.autocreatemvvm.camelCaseToUnderscore
import com.mfh.auto.create.mvvm.autocreatemvvm.file.BaseFileModel
import com.mfh.auto.create.mvvm.autocreatemvvm.utils.CreateFileUtils

object ActivityFileModel : BaseFileModel() {

    override fun buildFileName() = "${Config.name}Activity.kt"

    override fun buildContentString(directory: PsiDirectory): String {
        val name = Config.name
        val bindingName = "Activity${name}Binding"
        val viewModelName = "${name}ViewModel"
        val projectPackage = getProjectPackage(directory)
        return buildString {
            appendLine("package ${CreateFileUtils.getPackageByDirectory(directory)}")
            appendLine()
            appendLine("import android.content.Intent")
            appendLine("import android.content.Context")
            appendLine("import ${Config.libPackageName}.base.${Config.baseActivityName}")
            if (projectPackage.isNotEmpty()) {
                appendLine("import ${projectPackage}.R")
                appendLine("import ${projectPackage}.databinding.$bindingName")
            }
            appendLine()
            appendLine("/**")
            appendLine(" * @desc: ${Config.desc}")
            appendLine(" */")
            appendLine("class ${name}Activity : ${Config.baseActivityName}<${bindingName}, ${viewModelName}>() {")
            appendLine()
            appendLine("    override fun getViewModelClass() = ${viewModelName}::class.java")
            appendLine()
            appendLine("    override fun getLayoutId() = R.layout.activity_${name.camelCaseToUnderscore()}")
            appendLine()
            appendLine("    override fun initData() {")
            appendLine("        binding.viewModel = viewModel")
            appendLine("    }")
            appendLine()
            appendLine("    companion object {")
            appendLine("        @JvmStatic")
            appendLine("        fun start(context: Context) {")
            appendLine("            val starter = Intent(context, ${name}Activity::class.java)")
            appendLine("            context.startActivity(starter)")
            appendLine("        }")
            appendLine("    }")
            appendLine("}")
        }
    }

}